package com.r2dbc.controller;

import com.r2dbc.entity.UserEntity;
import com.r2dbc.repository.UserRepository;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;


@RestController
@RequestMapping("tag")
public class UserController {
    private final UserRepository userRepository;

    public UserController(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @GetMapping("/page")
    public Flux<UserEntity> info(@RequestParam(required = false) String name,
                                 @RequestParam(defaultValue = "0") Integer page,
                                 @RequestParam(defaultValue = "5") Integer size) {

        // 查询第 0 页(页码从 0 开始计算) 5 条名字含有 "mp" 的条目，并按创建时间倒序排列
        // StartingWith -> like x%
        // EndingWith   -> like %x
        // Containing   -> like %x%
        // 详见 https://blog.csdn.net/u013189824/article/details/89498219
         Pageable pageable = PageRequest.of(page, size);
        return userRepository.findByNameStartingWithOrderByIdDesc(name, pageable);
    }
}
